package com.zmht.beike.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.zmht.beike.core.BaseDAOImpl;
import com.zmht.beike.dao.TargetManagerDAO;
import com.zmht.beike.po.TAimtocollection;
import com.zmht.beike.po.TCaiminfo;
import com.zmht.beike.po.TIaiminfo;
import com.zmht.beike.vo.TargetConect;
import com.zmht.beike.vo.TargetVO;

public class TargetManagerDAOImpl extends BaseDAOImpl implements
		TargetManagerDAO {
	/*
	 * 我收藏的目标查询
	 * (non-Javadoc)
	 * @see com.zmht.beike.dao.TargetManagerDAO#getMyFavorTarget(com.zmht.beike.vo.TargetConect, int, int)
	 */
	public List getMyFavorTarget(TargetConect content,int startIndex,int pageSize) {
		if(content.getTargetType()==null){return new ArrayList();}
		//开始查询
		Session session = this.getSession();
		Criteria c = session.createCriteria(TAimtocollection.class);		
		
		//查询集体目标
		if(new Integer(1).equals(content.getTargetType())){
			//我的目标 //匹配老师
			if(null!=content.getTeacherId()&&!new Integer(0).equals(content.getTeacherId())){
				c.add(Restrictions.eq("collectionTeacherId", content.getTeacherId()+""));
			}
			
			
			//匹配年龄
			//if(null!=content.getAge()&&!new Integer(0).equals(content.getAge())){
			//	c.createCriteria("caiminfo", "ca").add(Restrictions.like("ca.paraValue", content.getAge()+"",MatchMode.ANYWHERE));
			//}
			//匹配目标属性 标准，自定义
			Criteria cc = c.createCriteria("caiminfo", "c");
			if(null!=content.getTargetOption()){
				System.out.println("标准，自定义目标收藏");
				cc.add(Restrictions.eq("c.type", content.getTargetOption()));
			}
			
			if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
				cc.add(Restrictions.ge("c.enactmentDate", content.getStartTime()));
			}
			if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
				cc.add(Restrictions.le("c.enactmentDate", content.getEndTime()));
			}
			/*if(null!=content.getAreaId()&&!new Integer(0).equals(content.getAreaId())){
				cc.add(Restrictions.eq("c.areaId", content.getAreaId()));
			}*/
			//按老师姓名模糊匹配
			Criteria ccc = cc.createAlias("teacher", "tc");
			if(null!=content.getTeacherName()&&!"".equals(content.getTeacherName())){
				ccc.add(Restrictions.like("tc.tname",content.getTeacherName(),MatchMode.ANYWHERE));
			}
			
			c.setFirstResult(startIndex);
			c.setMaxResults(pageSize);
			List<TAimtocollection> list = c.list();
			List<TargetVO> vos = new ArrayList();
			//Restrictions.like("teacherId", content.getTeacherId(), MatchMode.ANYWHERE);
			for(TAimtocollection t:list){
				TargetVO vo = new TargetVO();
				vo.setTargetName(t.getCaiminfo().getCaiminfoName());
				vo.setTargetId(t.getCaiminfo().getId());
				vo.setTeacherName(t.getCaiminfo().getTeacher().getTname());
				vo.setCtime(t.getCaiminfo().getEnactmentDate());
				vo.setTargetTypeName("集体目标");
				vo.setTargetOptionName(new Integer(0).equals(t.getCaiminfo().getType())?"标准目标":"自定义目标");
				vo.setAge(new Integer(t.getCaiminfo().getParaValue()));
				vo.setConcount(t.getCaiminfo().getConcount());
				
				vo.setCtime(t.getCaiminfo().getEnactmentDate());
				
				
				vos.add(vo);
			}
			return vos;
			
			//查询个别目标
		}else{
			//我的目标 //匹配老师
			if(null!=content.getTeacherId()&&!new Integer(0).equals(content.getTeacherId())){
				c.add(Restrictions.eq("collectionTeacherId", content.getTeacherId()+""));
			}
			//匹配目标属性 标准，自定义
			Criteria cc = c.createCriteria("iaiminfo", "c");
			if(null!=content.getTargetOption()){
				cc.add(Restrictions.eq("c.type", content.getTargetOption()));
			}
			
			if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
				cc.add(Restrictions.ge("c.enactmentDate", content.getStartTime()));
			}
			if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
				cc.add(Restrictions.le("c.enactmentDate", content.getEndTime()));
			}
			
			//按老师姓名模糊匹配
			Criteria ccc = cc.createAlias("teacher", "tc");
			if(null!=content.getTeacherName()&&!"".equals(content.getTeacherName())){
				ccc.add(Restrictions.like("tc.tname",content.getTeacherName(),MatchMode.ANYWHERE));
			}
			c.setFirstResult(startIndex);
			c.setMaxResults(pageSize);
			List<TAimtocollection> list = c.list();
			
			List<TargetVO> vos = new ArrayList();
			for(TAimtocollection t:list){
				TargetVO vo = new TargetVO();
				vo.setTargetId(t.getIaiminfo().getId());
				vo.setTargetName(t.getIaiminfo().getIaiminfoName());
				vo.setTeacherName(t.getIaiminfo().getTeacher().getTname());
				vo.setCtime(t.getIaiminfo().getEnactmentDate());
				vo.setTargetTypeName("个别目标");
				vo.setTargetOptionName(new Integer(0).equals(t.getIaiminfo().getType())?"标准目标":"自定义目标");
				vo.setConcount(t.getIaiminfo().getConcount());
				
				vos.add(vo);
			}
		
			return vos;
		}
		
	}
	/*
	 * 查询我收藏的目标记录总数
	 * (non-Javadoc)
	 * @see com.zmht.beike.dao.TargetManagerDAO#getMyFavorTargetTotal(com.zmht.beike.vo.TargetConect)
	 */
	public int getMyFavorTargetTotal(TargetConect content) {
		if(new Integer(1).equals(content.getTargetType())){
			Session session = this.getSession();
			Criteria c = session.createCriteria(TAimtocollection.class);
			//查询我收藏的集体目标
			if(null!=content.getTeacherId()&&!new Integer(0).equals(content.getTeacherId())){
				c.add(Restrictions.eq("collectionTeacherId", content.getTeacherId()+""));
			}
			
			
			//匹配年龄
			//if(null!=content.getAge()&&!new Integer(0).equals(content.getAge())){
			//	c.createCriteria("caiminfo", "ca").add(Restrictions.like("ca.paraValue", content.getAge()+"",MatchMode.ANYWHERE));
			//}
			//匹配目标属性 标准，自定义
			Criteria cc = c.createCriteria("caiminfo", "c");
			if(null!=content.getTargetOption()){
				System.out.println("标准，自定义目标收藏");
				cc.add(Restrictions.eq("c.type", content.getTargetOption()));
			}
			
			if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
				cc.add(Restrictions.ge("c.enactmentDate", content.getStartTime()));
			}
			if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
				cc.add(Restrictions.le("c.enactmentDate", content.getEndTime()));
			}
			/*if(null!=content.getAreaId()&&!new Integer(0).equals(content.getAreaId())){
				cc.add(Restrictions.eq("c.areaId", content.getAreaId()));
			}*/
			//按老师姓名模糊匹配
			Criteria ccc = cc.createAlias("teacher", "tc");
			if(null!=content.getTeacherName()&&!"".equals(content.getTeacherName())){
				ccc.add(Restrictions.like("tc.tname",content.getTeacherName(),MatchMode.ANYWHERE));
			}
			/*if(null!=content.getAreaId()&&!new Integer(0).equals(content.getAreaId())){
				cc.add(Restrictions.eq("c.areaId", content.getAreaId()));
			}*/			
			return c.list().size();
	
		}else{
			Session session = this.getSession();
			Criteria c = session.createCriteria(TAimtocollection.class);
			//我的目标 //匹配老师
			if(null!=content.getTeacherId()&&!new Integer(0).equals(content.getTeacherId())){
				c.add(Restrictions.eq("collectionTeacherId", content.getTeacherId()+""));
			}
			//匹配目标属性 标准，自定义
			Criteria cc = c.createAlias("iaiminfo", "c");
			if(null!=content.getTargetOption()){
				cc.add(Restrictions.eq("c.type", content.getTargetOption()));
			}

			if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
				cc.add(Restrictions.ge("c.enactmentDate", content.getStartTime()));
			}
			if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
				cc.add(Restrictions.le("c.enactmentDate", content.getEndTime()));
			}
			return c.list().size();

		}
	}
	/**
	 * 搜索我的目标
	 */
	public List getMyTarget(TargetConect content,int startIndex,int pageSize) {
		
		//如果是集体目标则 
		if(new Integer(1).equals(content.getTargetType())){
			Session session = this.getSession();
			Criteria c = session.createCriteria(TCaiminfo.class);
			//我的目标
			c.add(Restrictions.eq("teacherId", content.getTeacherId()));
			if(null!=content.getAge()&&!new Integer(0).equals(content.getAge())){
				c.add(Restrictions.eq("paraValue",content.getAge().toString()));
			}
			if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
				c.add(Restrictions.ge("enactmentDate", content.getStartTime()));
				//sql += " and t.enactmentDate>='"+content.getStartTime()+"'";
			}
			if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
			//	sql += " and t.enactmentDate<= '"+content.getEndTime()+"'";
				c.add(Restrictions.le("enactmentDate", content.getEndTime()));
			}
			
			/*if(null!=content.getAreaId()&&!new Integer(0).equals(content.getAreaId())){
				c.add(Restrictions.eq("areaId", content.getAreaId()));
			}*/
			c.setFirstResult(startIndex);
			c.setMaxResults(pageSize);
			List<TCaiminfo> list = c.list();
			List<TargetVO> vos = new ArrayList();
			//Restrictions.like("teacherId", content.getTeacherId(), MatchMode.ANYWHERE);
			for(TCaiminfo t:list){
				TargetVO vo = new TargetVO();
				vo.setTargetName(t.getCaiminfoName());
				vo.setTargetId(t.getId());
				vo.setCtime(t.getEnactmentDate());
				vo.setTeacherName(t.getTeacher().getTname());
				vo.setTargetTypeName("集体目标");
				vo.setTargetOptionName(new Integer(0).equals(t.getType())?"标准目标":"自定义目标");
				vo.setConcount(t.getConcount());
				vo.setAge(new Integer(t.getParaValue()));
				vos.add(vo);
			}
			return vos;
		}else{
			Session session = this.getSession();
			Criteria c = session.createCriteria(TIaiminfo.class);
			c.add(Restrictions.eq("teacherId", content.getTeacherId()));
			//Restrictions.like("insuranceId", content.getTeacherId(), MatchMode.ANYWHERE);
			//c.createAlias("ersInsuranceProduct", "product");
			
			c.setFirstResult(startIndex);
			c.setMaxResults(pageSize);
			List<TIaiminfo> list = c.list();
			List<TargetVO> vos = new ArrayList();
			for(TIaiminfo t:list){
				TargetVO vo = new TargetVO();
				vo.setTargetId(t.getId());
				vo.setTargetName(t.getIaiminfoName());
				vo.setCtime(t.getEnactmentDate());
				vo.setTeacherName(t.getTeacher().getTname());
				vo.setTargetTypeName("个别目标");
				vo.setTargetOptionName(new Integer(0).equals(t.getType())?"标准目标":"自定义目标");
				vo.setConcount(t.getConcount());
					
				vos.add(vo);
			}
			
			return vos;
		}
		
		
	
	}

	public int getMyTargetTotal(TargetConect content) {
		//如果是集体目标则 
		if(new Integer(1).equals(content.getTargetType())){
			Session session = this.getSession();
			Criteria c = session.createCriteria(TCaiminfo.class);
			//我的目标
			c.add(Restrictions.eq("teacherId", content.getTeacherId()));
			if(null!=content.getAge()&&!new Integer(0).equals(content.getAge())){
				c.add(Restrictions.eq("paraValue",content.getAge().toString()));
			}
			if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
				c.add(Restrictions.ge("enactmentDate", content.getStartTime()));
			
			}
			if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
			
				c.add(Restrictions.le("enactmentDate", content.getEndTime()));
			}
			/*if(null!=content.getAreaId()&&!new Integer(0).equals(content.getAreaId())){
				c.add(Restrictions.eq("areaId", content.getAreaId()));
			}*/
			return c.list().size();
	
		}else{
			Session session = this.getSession();
			Criteria c = session.createCriteria(TIaiminfo.class);
			Restrictions.eq("teacherId", content.getTeacherId());
			
			//Restrictions.like("insuranceId", content.getTeacherId(), MatchMode.ANYWHERE);
			//c.createAlias("ersInsuranceProduct", "product");
			
			return c.list().size();

		}
		
	}
	public boolean exqHQL(String hql) {
		try{
			this.getSession().createQuery(hql).executeUpdate();
			return true;
		}catch(Exception e){
			return false;
		}
	
	}

}
